/**
 * @project name:mail
 */
package com.situ.mail.shop.controller;

import com.situ.mail.base.BaseController;
import com.situ.mail.shop.pojo.Directory;
import com.situ.mail.shop.service.DirectoryService;
import com.situ.mail.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 商品 上下级表
 *
 * @Author : yun&bo
 * @CreateDate : ==>2020/12/24 23:47
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@Controller
@RequestMapping("/directory")
public class DirectoryController extends BaseController {
    /**
     * DIRECTORY_PAGERS = "pagers";                                   查出的集合
     * DIRECTORY_OBJ = "obj";                                         查出的单个元素
     * DIRECTORY_LIST = "admin/directory_list";                       跳一级表单展示页面
     * DIRECTORY_LIST_SON = "admin/directory_list_son";               跳儿子表单展示页面
     * DIRECTORY_LIST_GO = "directory/findBySql";                     前往跳转一级表单的Controller
     * DIRECTORY_LIST_GO_SON = "directory/sonFindBySql";              前往儿子表单展示页面Controller ？后面+father的id     添加新的元素后，仍然在增加前目录下
     * DIRECTORY_ADD_LIST = "admin/directory_add_list";               跳一级表单修改页面
     * DIRECTORY_ADD_SON_LIST = "admin/directory_add_son_list";       跳儿子表单修改页面
     * DIRECTORY_EDIT_LIST = "admin/directory_edit";                  跳修改页面
     */
    private static final String DIRECTORY_PAGERS = "pagers";
    private static final String DIRECTORY_LIST = "admin/directory_list";
    private static final String DIRECTORY_LIST_SON = "admin/directory_list_son";
    private static final String DIRECTORY_LIST_GO = "directory/findBySql";
    private static final String DIRECTORY_LIST_GO_SON = "directory/sonFindBySql";
    private static final String DIRECTORY_ADD_LIST = "admin/directory_add_list";
    private static final String DIRECTORY_ADD_SON_LIST = "admin/directory_add_son_list";
    private static final String DIRECTORY_EDIT_LIST = "admin/directory_edit";


    @Autowired
    private DirectoryService directoryService;


    /**
     * 分页查询类目列表
     */
    @RequestMapping("/findBySql")
    public String findBySql(Model model, Directory directory) {
        String sql = "select  ROW_ID, FATHER_ID, DIR_NAME, GOOD_COUNT,ACTIVE_FLAG, " +
                "CREATE_BY, CREATE_DATE, UPDATE_BY,UPDATE_DATE FROM TB_DIRECTORY WHERE ACTIVE_FLAG = 1 " +
                "AND FATHER_ID = -1  ORDER BY ROW_ID";
        Pager<Directory> pager = directoryService.findBySqlRerturnEntity(sql);
        model.addAttribute(DIRECTORY_PAGERS, pager);
        return DIRECTORY_LIST;
    }

    /**
     * 子分页查询类目列表
     */
    @RequestMapping("sonFindBySql")
    public String sonFindBySql(Model model, Long fatherId) {
        String sql = "select  ROW_ID, FATHER_ID, DIR_NAME, GOOD_COUNT,ACTIVE_FLAG, " +
                "CREATE_BY, CREATE_DATE, UPDATE_BY,UPDATE_DATE FROM TB_DIRECTORY WHERE ACTIVE_FLAG = 1 " +
                "AND FATHER_ID = " + fatherId + " ORDER BY ROW_ID";
        /**
         * 得到当前目录的所有子
         */
        Pager<Directory> pager = directoryService.findBySqlRerturnEntity(sql);
        /**
         * father 当前检索集合的父
         * supper 当前检索集合的爷爷
         */
        Directory father = directoryService.load(fatherId);
        Directory superFather = directoryService.load(father.getFatherId());
        directoryService.load(fatherId);
        model.addAttribute(DIRECTORY_PAGERS, pager);
        model.addAttribute("father", father);
        model.addAttribute("superFather", superFather);
        return DIRECTORY_LIST_SON;
    }


    /**
     * 转向添加一级分类页面
     */
    @RequestMapping("/add")
    public ModelAndView goAdd(ModelAndView modelAndView) {
        modelAndView.setViewName(DIRECTORY_ADD_LIST);
        return modelAndView;
    }


    @RequestMapping("/addson")
    public String goAddSon(Model model, Directory directory) {
        //拿到父ID
        model.addAttribute("directory", directory);
        return DIRECTORY_ADD_SON_LIST;
    }

    /**
     * 转向添加顶级分类页面
     */
    @RequestMapping("/doadd")
    public String doAdd(Directory directory) {
        System.out.println(directory+"=======================");
        directoryService.insert(directory);
        if (directory.getFatherId() == -1L) {
            return "redirect:/" + DIRECTORY_LIST_GO;
        } else {
            return "redirect:/" + DIRECTORY_LIST_GO_SON + "?fatherId=" + directory.getFatherId();
        }
    }


    /**
     * 转向 修改 目录页面
     */

    @RequestMapping("/edit")
    public String goEdit(Long rowId, Model model) {
        Directory directory = directoryService.load(rowId);
        model.addAttribute("directory", directory);
        return DIRECTORY_EDIT_LIST;
    }

    /**
     *
     */
    @RequestMapping("/doedit")
    public String doEdit(Directory directory) {
        System.out.println(directory);
        directoryService.updateById(directory);

        if (isEmpty(directory.getFatherId()) && directory.getFatherId() == -1L) {

            return "redirect:/" + DIRECTORY_LIST_GO;
        } else {
            return "redirect:/" + DIRECTORY_LIST_GO_SON + "?fatherId=" + directory.getFatherId();
        }
    }




    @RequestMapping("/delete")
    public String doDelete(Long rowId) {
        Directory directory = directoryService.load(rowId);
        directory.setActiveFlag(0);
        directoryService.updateById(directory);
        String sql = "update TB_DIRECTORY set ACTIVE_FLAG = 0 WHERE FATHER_ID = " + rowId;
        directoryService.updateBysql(sql);
            return "redirect:/" + DIRECTORY_LIST_GO;


    }






}
